package info.mykhaylov.mvc.services;

import info.mykhaylov.dao.BaseEntityDao;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

public interface DataService {

	<T>
	void create(final @NotNull T entity);

	@Nullable
	<T>
	T read(final @NotNull Class<T> clazz, final long id);

	@NotNull
	<T>
	List<? extends T> readAll(final @NotNull Class<T> clazz);

	<T>
	void update(final @NotNull T entity);

	<T>
	void delete(final @NotNull T entity);
}

@Service
class DaoDataService implements DataService {
	@Autowired
	BaseEntityDao baseEntityDao;

	@Override
	public <T> void create(final @NotNull T entity) {
		baseEntityDao.persist(entity);
	}

	@NotNull
	@Override
	public <T> List<? extends T> readAll(@NotNull Class<T> clazz) {
		return baseEntityDao.loadAll(clazz);
	}

	@Override
	@Nullable
	public <T> T read(final @NotNull Class<T> clazz, final long id) {
		return baseEntityDao.load(clazz, id);
	}

	@Override
	public <T> void update(final @NotNull T entity) {
		baseEntityDao.update(entity);
	}

	@Override
	public <T> void delete(final @NotNull T entity) {
		baseEntityDao.delete(entity);
	}
}
